import Article from './Article'
import ArticleComment from './ArticleComment'
import './BigModelConfig'
import BigModelConfig from './BigModelConfig'
import './db'
import sequelize from './db'
import KnowledgeBase from './KnowledgeBase'
import KnowledgeBaseFile from './KnowledgeBaseFile'
import './SiteConfig'
import TeachCase from './TeachCase'
import TeachCaseComment from './TeachCaseComment'
import User from './User'
import Question from './Question'
import UserAnswer from './UserAnswer'

// 文章-用户，一个用户有多个文章，文章属于一个用户
Article.belongsTo(User, {
    foreignKey: 'userId',
    as: 'user',
})
User.hasMany(Article, {
    foreignKey: 'userId',
    as: 'articles',
})

// 案例-用户，一个用户有多个案例，案例属于一个用户
TeachCase.belongsTo(User, {
    foreignKey: 'userId',
    as: 'user',
})
User.hasMany(TeachCase, {
    foreignKey: 'userId',
    as: 'teachCases',
})

// 知识库-文件，知识库有多个文件，文件属于一个知识库
KnowledgeBase.hasMany(KnowledgeBaseFile, {
    foreignKey: 'knowledgeBaseId',
    as: 'knowledgeBaseFiles',
})
KnowledgeBaseFile.belongsTo(KnowledgeBase, {
    foreignKey: 'knowledgeBaseId',
    as: 'knowledgeBase',
})

// 知识库-向量化模型，知识库属于一个向量模型，一个向量模型可以应用在多个知识库
KnowledgeBase.belongsTo(BigModelConfig, {
    foreignKey: 'embedModelId',
    as: 'embedModel',
})
BigModelConfig.hasMany(KnowledgeBase, {
    foreignKey: 'embedModelId',
    as: 'knowledgeBases',
})

// 知识库-教学案例，教学案例属于一个知识库，一个知识库可以应用在多个教学案例
KnowledgeBase.hasMany(TeachCase, {
    foreignKey: 'knowledgeBaseId',
    as: 'knowledgeBase',
})
TeachCase.belongsTo(KnowledgeBase, {
    foreignKey: 'knowledgeBaseId',
    as: 'knowledgeBase',
})

// 文章-评论-用户，评论-评论
User.hasMany(ArticleComment, {
    foreignKey: 'userId',
    as: 'articleComments',
})
ArticleComment.belongsTo(User, {
    foreignKey: 'userId',
    as: 'user',
})
Article.hasMany(ArticleComment, {
    foreignKey: 'articleId',
    as: 'articleComments',
})
ArticleComment.belongsTo(Article, {
    foreignKey: 'articleId',
    as: 'article',
})
ArticleComment.belongsTo(ArticleComment, {
    foreignKey: 'parentId',
    as: 'parent',
})
ArticleComment.hasMany(ArticleComment, {
    foreignKey: 'parentId',
    as: 'children',
})

// 教学案例-评论-用户，评论-评论
TeachCase.hasMany(TeachCaseComment, {
    foreignKey: 'teachCaseId',
    as: 'teachCaseComments',
})
TeachCaseComment.belongsTo(TeachCase, {
    foreignKey: 'teachCaseId',
    as: 'teachCase',
})
TeachCaseComment.belongsTo(User, {
    foreignKey: 'userId',
    as: 'user',
})
User.hasMany(TeachCaseComment, {
    foreignKey: 'userId',
    as: 'teachCaseComments',
})
TeachCaseComment.hasMany(TeachCaseComment, {
    foreignKey: 'parentId',
    as: 'children',
})
TeachCaseComment.belongsTo(TeachCaseComment, {
    foreignKey: 'parentId',
    as: 'parent',
})

// 问题-教学案例
TeachCase.hasMany(Question, {
    foreignKey: 'teachCaseId',
    as: 'questions',
})
Question.belongsTo(TeachCase, {
    foreignKey: 'teachCaseId',
    as: 'teachCase',
})

// 问题-用户答案
User.hasMany(UserAnswer, {
    foreignKey: 'userId',
    as: 'userAnswers',
})
UserAnswer.belongsTo(User, {
    foreignKey: 'userId',
    as: 'user',
})
Question.hasMany(UserAnswer, {
    foreignKey: 'questionId',
    as: 'userAnswers',
})
UserAnswer.belongsTo(Question, {
    foreignKey: 'questionId',
    as: 'question',
})

// sequelize.sync({
//     alter: true,
// })
